<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 检验值模型
 * @author www.dingwei.cn
 */

class ConsumerExperienceValueModel extends MY_Model{

	public $tbLog = 'consumer_experience_value_log';// 经验值奖励记录
	public $tbConsumer = 'consumer';
	public $type = array('1' => '注册赠送','2' => '签到','3' => '发布信息', '4' => '分享', '5' => '在线消费');

	function __construct(){
		parent::__construct();
	}

    /**
     * 获取详情
     * @param $id
     * @param bool $isNumber       true 获取当天赠送积分的数量
     * @return array
     * @author heyu
     */
    public function getLogInfo($id, $isNumber = false){
        if ($isNumber){
            $time = strtotime(date('Y-m-d', time()));
            $this->db->select('count(id) as number, type')
                ->group_by('type')
                ->where('addtime > ',$time);
            $tA = $this->getAll($this->tbLog);

            $data = array();
            if (!empty($tA)) {
                foreach ($tA as $k => $v) {
                    $data[$v['type']] = $v['number'];
                }
            }
            unset($tA);
            return $data;
        }
        if (!$id){
            return array();
        }
        $this->db->where(array('id' => $id));
        return $this->getOne($this->tbLog);
    }

	/**
	 * 新增|修改
	 * @param array $data 	数据
	 * @param int   $id
	 * @return boolean|boolean|number
	 * @author heyu
	 */
	public function save($data , $id = 0){
		if ($id){
			$this->db->where('id',$id);
			$query = $this->update($this->tbLog, $data);
			return ($query === false ? false : true);
		}else {
			return $this->insert($this->tbLog, $data);
		}
	}

    /**
     * 后台获取列表
     * @return array
     * @author heyu
     */
    public function adminGetList(){
        $consumerId = intval(get('consumer_id'));
        $type = intval(get('experience_type'));
        $experience_time_from = strtotime(get('experience_time_from'));
        $experience_time_to = strtotime(get('experience_time_to'));
        $iSortCol_0 = get('iSortCol_0');
        $sSortDir_0 = get('sSortDir_0');

        if(!$consumerId)return array();

        if ($type){
            $this->db->where('type', $type);
        }
        if($experience_time_from){
            $this->db->where('addtime >=', $experience_time_from);
        }
        if($experience_time_to){
            $this->db->where('addtime <', strtotime('+1day', $experience_time_to));
        }
        if ($iSortCol_0 == 2){
            $this->db->order_by('addtime '.$sSortDir_0);
        }else{
            $this->db->order_by('id desc ');
        }
        $this->db->where('consumer_id', $consumerId);
        return $this->pagination($this->tbLog);
    }

    /**
     * 经验奖励
     * @param $type int 1：注册赠送 2签到 3发布信息 4分享 5在线消费
     * 2 签到经验奖励数量(每日限一次)
     * 3 发布信息经验奖励数量(每日首次)
     * 4 分享经验奖励数量( 每日限5次)
     * @author heyu
     */
    public function _addExperienceValue($type, $consumerId, $userExperienceValue){
        if(!$type || !$consumerId)return array('status' => 0, 'message' => '经验奖励失败');
        // 签到赠送积分数量(每日限一次)
        get_instance()->loadModel('AdmSettingModel', 'mSetting', 'admin');
        get_instance()->loadModel('ConsumerUserModel', 'mUser', 'consumer');

        if ($type == 2){
            $name = 'sign_experience_value_number';
        }else if($type == 3){
            $name = 'publish_experience_value_number';
        }else if($type == 4){
            $name = 'share_experience_value_number';
        }else{
            return;
        }
        // 查看当日是否已赠送积分
        $logInfo = $this->getLogInfo(0, true);
        if (!empty($logInfo)){
            $count = isset($logInfo[$type]) ? $logInfo[$type] : 0;
            if ($type == 5) {
                if ($count >= 5) {
                    return;
                }
            }else if ($count){
                return;
            }
        }
        $number = $this->mSetting->get($name); // 发布信息赠送数量
        if ($number > 0) {
            $integral = $this->mUser->save(array('experience_value' =>$userExperienceValue+$number), $consumerId);
            if (!$integral) {
                return array('status' => 0, 'message' => '经验奖励失败');
            }
            // 添加积分记录
            $logData = array(
                'consumer_id' => $consumerId,
                'type' => $type,
                'experience_value' => $number,
                'addtime' => time(),
            );
            $integralLog = $this->save($logData);
            if (!$integralLog) {
                return array('status' => 0, 'message' => '添加经验奖励记录失败');
            }
        }
        return array('status' => 1, 'message' => '经验奖励成功');
    }


}
